/************************************************************************
 * Este archivo, representa todo lo relacionado a la interfaz para el
 * manejo de ingresos y egresos de la caja
 * 
 * @system: veco-system
 * @description: Sistema de control vehicular
 * @version: 1.0
 * @Copyright © 2013 
 * 
 * @authores:
 * 
 *      > Alberione, Fernando    <falberione_037@gmail.com>
 * 
 *      > Boretto, Martin        <martinboretto@gmail.com>
 * 
 *      > Lenarduzzi, Federico   <fedeatenas@gmail.com>
 * 
 *      > Ocampo, Emanuel        <eocampo86@gmail.com>
 *
 *************************************************************************/

package Interfaces;

import SistemaCocheras.Tickets;
import SistemaCocheras.manejadorCaja;
import SistemaCocheras.manejadorEstadia;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Connection;
import javax.swing.JOptionPane;


public class movimientoCaja extends javax.swing.JFrame {
    Connection conexion = null;
    String dniUsuarioActual = "";

    
    // ---------------------------------------------------------
    // ------- Seccion de constructores de la clase
    // --------------------------------------------------------- 
    
    /**
     * Constructor vacio
     */
    public movimientoCaja() {
        initComponents();
        centrarJFrame();
    }

    
   /**
    * Metodo encargado de crear el frame "Movimiento Caja"
    * @param con Representa la conexion con la base de datos
    * @param dni Representa el dni del usuario que opera la caja
    */
    public movimientoCaja(Connection con,String dni) {
        this.conexion = con;
        this.dniUsuarioActual = dni;
        initComponents();
        centrarJFrame();
    }// Fin movimientoCaja
    
    
    /** Este metodo es llamado desde el constructor para 
     * inicializar el form.
     * CUIDADO: NO modifique este codigo. El contenido de este metodo
     * es siempre regenerado por el editor de form.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonIngreso = new javax.swing.JToggleButton();
        buttonEgreso = new javax.swing.JToggleButton();
        buttonCancelar = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("B&B - Movimiento de caja");

        buttonIngreso.setText("Ingreso");
        buttonIngreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonIngresoActionPerformed(evt);
            }
        });

        buttonEgreso.setText("Egreso");
        buttonEgreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEgresoActionPerformed(evt);
            }
        });

        buttonCancelar.setText("Cancelar");
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Descripcion del movimiento de caja:");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Importe:");

        jLabel3.setText("$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonIngreso, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(buttonEgreso, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(buttonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonCancelar, buttonEgreso, buttonIngreso});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(buttonEgreso)
                            .addComponent(buttonIngreso)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addComponent(buttonCancelar)))
                .addGap(22, 22, 22))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonCancelar, buttonEgreso, buttonIngreso});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    // ---------------------------------------------------------
    // ------- Seccion de metodos privados
    // ---------------------------------------------------------  
    
    /**
     * Metodo encargado de realizar un egreso de la caja
     * @param evt Evento que representa el click en el boton Egreso
     */
    private void buttonEgresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEgresoActionPerformed
        try {
            String descripcion = this.jTextField1.getText();
            double monto = Double.parseDouble(this.jTextField2.getText());
            manejadorCaja mc = new manejadorCaja(this.conexion);
            manejadorEstadia me = new manejadorEstadia(this.conexion);
            String fecha = me.calcularFecha();
            String hora  = me.calcularHora();
            mc.insertarEgreso(descripcion, this.dniUsuarioActual, monto, fecha, hora);
            JOptionPane.showMessageDialog(null, "Se ha realizado correctamente el movimiento de caja (egreso)", "Sistema de Cocheras B&B", JOptionPane.INFORMATION_MESSAGE);
            Tickets ticket = new Tickets(this.conexion);
            ticket.imprimirTicketMovEgreso(descripcion, this.dniUsuarioActual, monto, fecha, hora);
            this.dispose();
        } 
        catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Ha ocurrido un error interno :" + ex, "Sistema de Cocheras B&B", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_buttonEgresoActionPerformed

    /**
     * Metodo encargado de cancelar un movimiento de caja
     * @param evt Evento que representa el click en el boton Cancelar
     */
    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCancelarActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
       
    }//GEN-LAST:event_jTextField1ActionPerformed

    /**
     * Metodo encargado de realizar un ingreso de la caja
     * @param evt Evento que representa el click en el boton Ingreso
     */
    private void buttonIngresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonIngresoActionPerformed
        try {
            String descripcion = jTextField1.getText();
            double monto = Double.parseDouble(jTextField2.getText());
            manejadorCaja mc = new manejadorCaja(this.conexion);
            manejadorEstadia me = new manejadorEstadia(this.conexion);
            String fecha = me.calcularFecha();
            String hora  = me.calcularHora();
            mc.insertarIngreso(descripcion, this.dniUsuarioActual, monto, fecha, hora);
            JOptionPane.showMessageDialog(null, "Se ha realizado correctamente el movimiento de caja (ingreso)", "Sistema de Cocheras B&B", JOptionPane.INFORMATION_MESSAGE);
            Tickets ticket = new Tickets(this.conexion);
            ticket.imprimirTicketMovIngreso(descripcion, this.dniUsuarioActual, monto, fecha, hora);
            this.dispose();
        } 
        catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Ha ocurrido un error interno :" + ex, "Sistema de Cocheras B&B", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_buttonIngresoActionPerformed

    /**
     * Metodo encargado de ubicar el frame en el centro de la pantalla
     */
    private void centrarJFrame(){
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frame =this.getSize();
        this.setLocation((pantalla.width-frame.width)/2, (pantalla.height-frame.height)/2);
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new movimientoCaja().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton buttonCancelar;
    private javax.swing.JToggleButton buttonEgreso;
    private javax.swing.JToggleButton buttonIngreso;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
